﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using FYPDemo;

namespace FYPDemo
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["username"] != null)
                {
                    addNewPost.Enabled = true;
                }

                Session["page"] = "forum";

                bindDDLCategory();
                GetTitle(1);
            }
        }

        protected void GetTitle(int category)
        {
            DataTable dt = VirtualCommunication.getTitle(category);

            if (dt.Rows.Count > 0)
            {
                this.TitleGrid.DataSource = dt;
                this.TitleGrid.DataBind();
                TitleGrid.Style.Add("display", "block");
                ForumList.Style.Add("display", "none");
            }

            else
            {
                TitleGrid.Style.Add("display", "none");
                ForumList.Style.Add("display", "block");
            }
        }

        protected void TitleGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.TitleGrid.PageIndex = e.NewPageIndex;
            //GetTitle();
        }

        protected void addNewPost_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/addNewPost.aspx");
        }

        protected void TitleGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            string title = TitleGrid.SelectedDataKey.Value.ToString();

            Session["Title"] = title;

            Response.Redirect("~/Comment.aspx?Title=" + title);
        }

        protected void bindDDLCategory()
        {
            DataTable dt = VirtualCommunication.listForumCategory();

            ddlCategory.DataSource = dt;
            ddlCategory.DataTextField = "Category";
            ddlCategory.DataValueField = "ID";
            ddlCategory.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int category = Convert.ToInt32(ddlCategory.SelectedValue);
            GetTitle(category);
        }
    }
}